$(function() {
    if ($.getUrlParam("platform") == "android" || $.getUrlParam("platform") == "ios") {
        window.isWebView = true;
    }
    var init = function() {
        var cardIsture = true;
        var notWinning = ["0","1","2"]
        var winningList = [
            "15198005668506353835023",
            "15198008121262654684316",
            "15198010248282132408860"
        ];
        //转动
        var rolate = function (id,name) {

            if(id == "1030"){ //未中奖
                id = notWinning[Math.floor(Math.random() * notWinning.length)];
            }
            else if(winningList.indexOf(id) != -1){ //中奖

            }
            else{
                id = "3";
            }

            switch (id) {
                case "15198005668506353835023": //预览:15178183619949228389367 现网:15198005668506353835023
                    rotateFunc("15198005668506353835023", 0, '咪咕视频体验会员卡3天',name);
                    break;
                case "0":
                    rotateFunc("0", 45, '好可惜~');
                    break;
                case "15198010248282132408860":
                     rotateFunc("15198010248282132408860", 90, '1G7天流量包-广东',name);
                     break;
                case "1":
                    rotateFunc("1", 135, '再接再厉');
                    break;
                case "15198008121262654684316": // 预览:15178184358596064042466 现网：15198008121262654684316
                    rotateFunc("15198008121262654684316", 180, '咪咕视频体验会员卡7天',name);
                    break;
                case "15198010248282132408860+":
                     rotateFunc("15032839660571073886673+", 225, '1G7天流量包-广东',name);
                     break;
                case "2":
                    rotateFunc("2", 270, '继续加油');
                    break;
                case "3":
                    rotateFunc("2", 270, '继续加油');
                    //rotateFunc("3", 315, '3G定向流量',name);
                    break;
            }
        };
        //
        var rotateFunc = function (awards, angle, text,name) {
            var $btn = $('.playbtn');
            $btn.stopRotate();
            $btn.rotate({
                angle: 0,
                duration: 3000, //旋转时间
                animateTo: angle + 1440, //让它根据得出来的结果加上1440度旋转
                callback: function () {
                    cardIsture = true; // 标志为 执行完毕
                    if(notWinning.indexOf(awards) != -1){
                        $(".div").css("background-image", "url(img/kexi.png)").find($(".cont")).text("运气在未来得转角等你，再试试~")
                        $(".pop").show();
                        $(".div .btn").hide();
                        $(".div .btn2").show();
                    }
                    else{
                        $(".div").css("background-image", "url(img/goodluck.png)").find($(".cont")).text(name + "入袋,现在就激活使用吧~")
                        $(".div .btn").show();
                        $(".div .btn2").hide();
                        $(".pop").show();
                    }
                }
            });
        };
        //初始化位置
        var initRotate = function () {
            var $btn = $('.playbtn');
            $btn.stopRotate();
            $btn.rotate({
                angle: 0,
                duration: 0, //旋转时间s
                animateTo:0//让它根据得出来的结果加上1440度旋转
            });
            cardIsture = true;
        }

        var app = new Vue({
            el: "#sign",
            data: {
                arr: [1, 2],
                signeddays: "0", //已经签到天数
                issigned: false, //今天是否签到
                award: "", //奖品
                gonggao: "", //中奖公告
                rules: "", //活动规则
                movies: [], //热门电影
                time:"",//活动时间
                remainTimes: "" //剩余抽奖次数
            },
            created: function() {
                var isLogin = $("#isLogin").val();
                var _this = this;
                if(isLogin != "1"){
                    if (window.isWebView) {
                        $.startLogin(function(data) {
                            alert(JSON.stringify(data));
                        });
                    } else {
                        //alert('未登录！');
                        location.href="https://passport.migu.cn/login?sourceid=203004&apptype=2&forceAuthn=true&isPassive=false&authType=&display=&callbackURL=" + window.location.href + "&relayState=login"
                    }
                }
                $.ajax({
                    url: " /promactivity/activity/getSignDay",
                    type: "post",
                    data: {
                        //现网的
                         intfId: "15223217719560844753682890866944",//现网："15223217719560844753682890866944" 预览："15178199224847728412564217604833"
                        //预览的
                       // intfId : "15178199224847728412564217604833"
                    },
                    beforeSend : function (xhr) {
                        //xhr.setRequestHeader("aaa", "fasdfadf")
                        //xhr.setRequestHeader("Cookie", "111")
                        //console.log(xhr.getResponseHeader("Cookie"))
                    },
                    dataType: "json",
                    success: function(data) {
                        if (data.code == "1") {
                            _this.signeddays = data.tag.day;
                            _this.issigned = data.tag.signed;
                        }
                    }
                })
                $.ajax({
                    url: "sign_data.jsp",
                    type: "post",
                    data: {
                        nodeId: "70050274"
                    },
                    dataType: "json",
                    success: function(data) {
                        console.log(data[0])
                        _this.remainTimes = data[0].remainTimes;
                        for (i = 0; i < data.length; i++) {
                            if (data[i].name == "中奖公告") {
                                _this.gonggao = data[i].detail[0].rule;
                            } else if (data[i].name == "活动规则") {
                                _this.rules = data[i].detail[0].rule;
                            } else if (data[i].name == "热门大片") {
                                _this.movies = data[i].detail;
                            }else if(data[i].name == "活动时间"){
                                _this.time = data[i].detail[0].rule;
                            }
                        }
                    }
                })
            },
            mounted : function () {

            },
            methods: {
                sign: function() { //签到
                    var _this = this;
                    //现网的
                     var intfId = '15223217719560844753682890866944'; //现网："15223217719560844753682890866944" 预览："15178199224847728412564217604833"
                     var signKey = "4b3a3d26-819d-4c75-ab81-277ccae980c3"; //现网："4b3a3d26-819d-4c75-ab81-277ccae980c3",预览："181c1071-7121-46f2-8031-943ff72df1bc"
                    //预览的
                    //var intfId = '15178199224847728412564217604833'; //现网："15223217719560844753682890866944" 预览："15178199224847728412564217604833"
                    //var signKey = "181c1071-7121-46f2-8031-943ff72df1bc"; //现网："4b3a3d26-819d-4c75-ab81-277ccae980c3",预览："181c1071-7121-46f2-8031-943ff72df1bc"
                    var imei = 10000,
                        idfa = "",
                        res = "",
                        sign = "";
                    var s = {
                        "mobile": $("#mobile").val()
                    };
                    $.ajax({
                        url: "/promactivity/queryAct/getToken",
                        type: "POST",
                        dataType: "JSON",
                        async: true,
                        data: {
                            intfId: intfId,
                            signKey: signKey,
                            isDefault: 0
                        },
                        beforeSend: function(jqXHR) {},
                        success: function(data, status, jqXHR) {
                            var code = data.code;
                            if (code == "1014") {
                                location.href = "https://passport.migu.cn/login?sourceid=203004&apptype=2&forceAuthn=true&isPassive=false&authType=&display=&callbackURL=" + window.location.href + "&relayState=login";
                            } else {
                                res = "userId=" + data.userId + "&intfId=" + intfId + "&imei=" + imei + "&idfa=" + idfa + "&token=" + data.tag + "&signKey=" + signKey;
                                sign = $.md5(res);
                                console.log(res);
                                console.log(sign)
                                var body = JSON.stringify(s);
                                $.ajax({
                                    url: "/promactivity/activity/execute",
                                    type: "POST",
                                    dataType: "JSON",
                                    async: true,
                                    data: {
                                        intfId: intfId,
                                        sign: sign,
                                        imei: 10000,
                                        from: "CMVIDEO",
                                        httpbody: body
                                    },
                                    beforeSend: function(jqXHR) {},
                                    success: function(data1, status, jqXHR) {
                                        if (data1.code == "2") {
                                            _this.signeddays = data1.tag.day;
                                            if (data1.tag.day == "3") {
                                                _this.remainTimes = Number(_this.remainTimes) + 3
                                            }
                                            _this.issigned = true;
                                        }
                                    }
                                })
                            }
                        }
                    })
                },
                alert: function() {
                    alert("您今日已经领取过")
                },
                getawardtimes: function() {

                },
                tosecond: function() {
                    location.href = "rewardsCenterPage.jsp"

                },
                close: function() {
                    initRotate();
                    $(".pop").hide();
                },
                jump: function(cid,img) {
                    if (window.isWebView) {
                        $.playDetail(cid, 0, img);
                    } else {
                        location.href = "/wap/resource/migu/miguH5/detail/detail.jsp?cid=" + cid;
                    }
                },
                getaward: function() { //抽奖
                    if(!cardIsture){
                        return;
                    }
                    cardIsture = false;

                    var _this = this;
                    //现网的
                     var intfId = '15223218746323693796400942397916'; //现网："15223218746323693796400942397916" 预览："15178200023723551118511001391107"
                     var signKey = "ab813b97-ca8f-46b8-9a33-dd23920b8449"; //现网："ab813b97-ca8f-46b8-9a33-dd23920b8449" 预览:"f5b81174-2fd4-491b-836f-8c58e5320fb1"
                    //预览的
                    //var intfId = '15178200023723551118511001391107'; //现网："15223218746323693796400942397916" 预览："15178200023723551118511001391107"
                    //var signKey = "f5b81174-2fd4-491b-836f-8c58e5320fb1"; //现网："ab813b97-ca8f-46b8-9a33-dd23920b8449" 预览:"f5b81174-2fd4-491b-836f-8c58e5320fb1"
                    var imei = 10000,
                        idfa = "",
                        res = "",
                        sign = "";
                    var s = {
                        "mobile": $("#mobile").val()
                    };
                    $.ajax({
                        url: "/promactivity/queryAct/getToken",
                        type: "POST",
                        dataType: "JSON",
                        async: true,
                        data: {
                            intfId: intfId,
                            signKey: signKey,
                            isDefault: 0
                        },
                        beforeSend: function(jqXHR) {},
                        success: function(data, status, jqXHR) {
                            var code = data.code;
                            if (code == "1014") {
                                location.href = "https://passport.migu.cn/login?sourceid=203004&apptype=2&forceAuthn=true&isPassive=false&authType=&display=&callbackURL=" + window.location.href + "&relayState=login";
                            } else {
                                res = "userId=" + data.userId + "&intfId=" + intfId + "&imei=" + imei + "&idfa=" + idfa + "&token=" + data.tag + "&signKey=" + signKey;
                                sign = $.md5(res);
                                console.log(res);
                                console.log(sign)
                                var body = JSON.stringify(s);
                                $.ajax({
                                    url: "/promactivity/activity/execute",
                                    type: "POST",
                                    dataType: "JSON",
                                    async: true,
                                    data: {
                                        intfId: intfId,
                                        sign: sign,
                                        imei: 10000,
                                        from: "CMVIDEO",
                                        httpbody: body
                                    },
                                    beforeSend: function(jqXHR) {},
                                    success: function(data1, status, jqXHR) {
                                        console.log(data1);
                                        if (data1.code == "1") { //成功中奖
                                            _this.award = data1.tag.wareName;
                                            _this.remainTimes--;
                                            var wareId = data1.tag.wareId
                                            rolate(wareId,data1.tag.wareName);
                                        } else if (data1.code == "1025") { //抽奖次数不够
                                            cardIsture = true;
                                            $(".div").css("background-image", "url(img/useup.png)").find($(".cont")).text("来日再战，坚持不懈，礼物入袋~")
                                            $(".pop").show();
                                            $(".div .btn").hide();
                                            $(".div .btn2").show();
                                            _this.remainTimes = 0;
                                        } else if (data1.code == "1030") {
                                            rolate("1030")
                                            _this.remainTimes--;
                                        }
                                    }
                                })
                            }
                        }
                    })
                }
            }
        })
    };
    if (window.isWebView) {
        window.mnWebMain.init(function() {
            init();
        })
    } else {
        init();
    }
})